<template>
  <el-dialog title="添加学生" :visible.sync="isAddStudent" width="40%" :before-close="handleClose" :close-on-click-modal='false'>
    <el-form ref="form" :model="form" label-width="80px" label-position="top" size="small" :rules="rules">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="姓名：" prop="real_name">
            <el-input v-model="form.real_name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别：" prop="sex">
            <el-select v-model="form.sex" placeholder="请选择性别">
              <el-option label="男" value="1"></el-option>
              <el-option label="女" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="账号：" prop="account">
            <el-input v-model="form.account"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密码：">
            <el-col :span="18" :xl="{span: 20}">
              <el-input v-if="isResetPassWord === false" type="password" v-model="form.pwd"></el-input>
              <el-input v-else type="text" v-model="form.pwd"></el-input>
            </el-col>
            <el-col :span="3" :xl="{span: 4}">
              <el-button type="text" @click="resetPassWord">重置密码</el-button>
            </el-col>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="学籍号：">
            <el-input v-model="form.student_id"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生日：">
            <el-date-picker v-model="form.birthday" type="date" placeholder="选择出生日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd"> </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="所属层级：" prop="flag">
            <el-cascader :options="options" v-model="form.options" :props="{value:'id',label:'title',children:'children'}" placeholder="请选择层级"> </el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态：" prop="flag">
            <el-select v-model="form.flag" placeholder="请选择状态">
              <el-option label="在读" value="1"></el-option>
              <el-option label="休学" value="2"></el-option>
              <el-option label="离校" value="3"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close()">取 消</el-button>
      <el-button type="primary" @click="checkFn()">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {APIALLLIST, APIADDSTUDENT} from '@/common/api.js'
export default {
  props: ['isAddStudent', 'schoolId'],
  data () {
    return {
      isResetPassWord: false,
      form: {
        real_name: '',
        account: '',
        sex: '',
        options: [],
        pwd: ''
      },
      rules: {
        real_name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'blur' }
        ],
        flag: [
          { required: true, message: '请选择层级', trigger: 'blur' }
        ]
      },
      options: []
    }
  },
  created () {
    this.allList()
  },
  methods: {
    allList () {
      APIALLLIST({
        school_id: this.schoolId
      }).then((res) => {
        this.options = res.data
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    handleClose () {
      this.$emit('noAddStudent')
    },
    close (type) {
      this.$emit('noAddStudent', type)
    },
    resetPassWord () {
      this.isResetPassWord = true
      this.form.pwd = '123456'
    },
    checkFn () {
      if (this.form.real_name === '') {
        this.$message.warning('姓名不能为空')
        return false
      }
      if (this.form.sex === '') {
        this.$message.warning('性别不能为空')
        return false
      }
      if (this.form.account === '') {
        this.$message.warning('账号不能为空')
        return false
      }
      if (this.form.options === []) {
        this.$message.warning('所属层级不能为空')
        return false
      }
      this.submit()
    },
    submit () { // 提交
      APIADDSTUDENT({
        school_id: this.schoolId,
        real_name: this.form.real_name,
        account: this.form.account,
        pwd: this.form.pwd,
        birthday: this.form.birthday,
        flag: this.form.flag,
        student_id: this.form.student_id,
        sex: this.form.sex,
        section_id: this.form.options !== [] ? this.form.options[0] : '',
        grade_id: this.form.options !== [] ? this.form.options[1] : '',
        class_id: this.form.options !== [] ? this.form.options[2] : ''
      }).then((res) => {
        if (res.status === 1) {
          this.$message.success(res.msg)
          this.close(1)
        } else {
          this.$message.error(res.msg)
        }
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    }
  }
}
</script>
